<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" />
	   	<title>{$Title}</title>
	    <link href="/static/weike/css/style.css" rel="stylesheet" />
	    <script type="text/javascript" src="/static/js/jquery.js"></script>
	    <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
	</head>
	<body class="weke_zhibo" id="{$identify}">
		<aside class="aside none" id="loading">
			<div>
				<span></span><span></span><span></span><span></span><span></span><span></span>
				<span></span><span></span><span></span><span></span><span></span><span></span>
			</div>
		</aside>
		<header>
			<article>
				<div>
					<img src="{$ListTeacher['headpic']}" />
				</div>
				<div>{$ListTeacher['nickname']}</div>
				<div class="student">
					<input type="checkbox" onclick="follow(this)" id="guanzhu" />
					<span></span>
				</div>
			</article>
			<aside class="student_none weke_head_asd" value="{$is_run}"><!--value为0或1;0表示未播过，1表示已播过-->
				<p></p>
			</aside>
		</header>
		<div class="scroll_div">
			<article class="art_scroll">
				<aside class="hdp_banner">
					<article>
					{volist name="RoomLunbo" id="vo"}
						<div><img src="{$vo.img_cover}" /></div>
					{/volist}	
						</article>
					<aside>
						<div><div><span class="page">1</span>/{$room_lunbo_count}</div></div>
						<div>
							<button class="page"><</button>
							<button class="page">></button>
						</div>
					</aside>					
				</aside>
				<div>
					<aside class="class_know">
						<div>
							<p>讲课须知</p>
							<div class="close">×</div>
						</div>
						<article>
							1.直播时请保持设备网络通畅<br />
							2.请提前测试录音效果<br />
							3.录音时请长按录音键进行录音<br />
							4.一条语音最长时长为60s，请把握好讲课节奏<br />
							5.直播不宜超过2小时,不得少于1分钟,注意把握讲课时长
						</article>	
					</aside>
					<article class="content">
						<aside class="enter_zhibo">
							<div>
								<img src="{$ListTeacher['headpic']}" />
							</div>
							<div>
								<p>{$ListTeacher['nickname']}</p>
								<div>
									<article>
										<div>
											<img src="{$ListTeacher['headpic']}" />
										</div>
										<div>
											<header>{$ListTeacher['nickname']}</header>
											<p>直播间</p>
										</div>
									</article>
									<p onclick="javascript:window.location.href='http://mg.bonayiyou.com/index/home/person_zhibo.html?user_id={$RoomUserId}'">查看</p>
								</div>
							</div>
						</aside>
						<section onclick="window.location.href='http://mg.bonayiyou.com/index/home/person_invite.html?room_id={$RoomId}&user_id={$UserId}'">
							<div>
								<img src="{$ListTeacher['headpic']}" />
							</div>
							<div>
								<p>{$ListTeacher['nickname']}</p>
								<div class="s_div" id="share">
									<span>大家点击邀请朋友来收听直播</span>
								</div>
							</div>
						</section>					
					   {volist name="ListVoice" id="vo"}
						<section>
							<div>
								<img src="{$ListTeacher['headpic']}" />
							</div>
							<div>
								<p>{$ListTeacher['nickname']}</p>
								<div style="width:{$vo.t|voice_length}%;" class="s_div talk">
									<div></div><article>
										<input type="radio" name="audio"  onclick="audio1(this)"  number="{$vo.number}" sort="{$vo.sort}" /><audio name="{$method}" src="{$vo.tem_voice}" controls="controls"></audio>
									</article><aside></aside><div><span>{$vo.t}</span>''</div>
								</div>
							</div>
						</section>
					{/volist}	
					</article>
				</div>
			</article>
			<div class="float">
				<input type="checkbox" />
				<aside></aside>
				<div>
					<footer>
						<p>{$CountNumber|add_wan}</p>
					</footer>
					<article class="float_art">
					{volist name="ListDanmu" id="vo" length='4'}
						<section>
							<div>
								<img src="{$vo.headimgurl}" />
							</div>
							<p>
								{$vo.content}
							</p>
						</section>
					{/volist}	
					</article>
				</div>
			</div>
			<aside class="scroll_show" ><a >回到播放位置</a></aside>
			<footer class="footer">
				<input type="checkbox" class="check student_none"/>
				<div>
					<div  class="student_none"><div></div></div>
					<input placeholder="发送文字" class="wk_item student_none" />
					<input placeholder="发送文字" class="wk_item student" />
					<button class="wk_senditem">发送</button>
					<div class="luyin">
						<button>长按录音</button>
						<aside>
							<p>录音中...<span class="w_time"></span></p>
							<div><div></div></div>
						</aside>
					</div>
					<div class="danmu">
						<input type="checkbox" />
						<article class="danmu_li">
							<div>
								<header>
									<header>最新评论（{$count_pinglun}）</header>
									<div class="close">×</div>
								</header>
								<article id="add_danmu">
									<div class="add_no">
									   {volist name="ListText" id="vo"}
										<section>
											<div>
												<img src="{$vo.headimgurl}" />
											</div>
											<div>
												<p>{$vo.nickname}</p>
												<div>
													{$vo.content}
												</div>
												<p><span>{$vo.create_time|date="Y-m-d",###}</span><span>{$vo.create_time|date="G:i:s",###}</span></p>
											</div>
										</section>
										{/volist}
									</div>
								</article>
								<footer class="student">
									<input placeholder="发表弹幕" type="text" class="stu_item" />
									<button class="stu_senditem">发送</button>
								</footer>
							</div>
						</article>
						<div></div>
					</div>
				</div>
			</footer>
		</div>
			</body>	
</html>
<script type="text/javascript">
var htmfontsi = document.body.clientWidth,
htmfontsize = parseFloat(htmfontsi)/750*100;
document.getElementsByTagName("html")[0].style.fontSize=htmfontsize+"px";
</script>
<script type="text/javascript">
        //调用微信JSSDK
 //js注入
  wx.config({
    debug: false,
    appId: "{$SignPackage['appId']}",
    timestamp: "{$SignPackage['timestamp']}",
    nonceStr: "{$SignPackage['nonceStr']}",
    signature: "{$SignPackage['signature']}",
    jsApiList: [
      // 所有要调用的 API 都要加到这个列表中
      "startRecord",
      "stopRecord",
      "onVoiceRecordEnd",
      "playVoice",
      "pauseVoice",
      "stopVoice",
      "onVoicePlayEnd",
      "uploadVoice",
      "downloadVoice",
      "getLocalImgData"
    ]
  });
  wx.ready(function () {
  //当语音播放完毕的时候
  wx.onVoicePlayEnd({
    success: function (res) {
        var localId = res.localId; // 返回音频的本地ID
    }
});

  });
//alert(localStorage.rainAllowRecord);    
//console.log(localStorage.rainAllowRecord);  
//定义全局变量
var START=0;
var END  =0;
var sort ="{$MaxSort}";
var timeout;
//上一个播放视频的localid
var OldLocalid='';
//记录上个音频播放的对象
var OldVoice=null;
// 监听端口
// websocket= window.WebSocket || window.MozWebSocket;
ws = new WebSocket("ws://120.25.242.117:8282");
// // 绑定分组的ajaxURL
var ajaxUrl="/index/gatewaywork/bind";
// // 发送消息的ajaxURL
var ajaxMsgUrl="/index/gatewaywork/send";
// 通过房间号进行分组
var roomId="{$RoomId}";
// 获取当前登录用户的guid，用于标识是自己发送的信息
var loginUser="{$UserId}";
// 获取当前房间号的主播的uid，用于标识是主播发送的信息
var roomUser="{$RoomUserId}";
var nickName="{$NickName}";
var headPic ="{$HeadPic}";
// 服务端主动推送消息时会触发这里的onmessage
ws.onmessage = function(e){
    console.log(e);
    // json数据转换成js对象
    var data = eval("("+e.data+")");
    var type = data.type || '';
    switch(type){
        // Events.php中返回的init类型的消息，将client_id发给后台进行uid绑定
        case 'init':
            // 利用jquery发起ajax请求，将client_id发给后端进行uid绑定
            $.post(ajaxUrl,{client_id:data.client_id,room_id:roomId}, function(data){
                // console.log(data);
            }, 'json');
            break;
        // 当mvc框架调用GatewayClient发消息时直接alert出来
        default :
            // 如果接受到的是主播发过来的,主播自己接受不在重复添加到面板
            if(data.FromUserId == roomUser&&data.FromUserId!=loginUser){
            	//如果是文字信息
            	if(data.Type==1)
            	{
                addMsgToZhuban(data);
                }
                //如果是音频信息
                else if(data.Type==2)
                {
                addMp3ToZhuban(data); 	
                }
                //提醒学生老师正在输入
                else if(data.Type==3)
                {
                remberTeacherHander(data);
                }
                break;
          
            }
            //如果只是同学发的弹幕信息,同学也不接受自己发送的信息
            else if(data.FromUserId!=roomUser&&data.FromUserId!=loginUser){                            
                addMsgToDanmu(data);
            }
            break;
    }
};
//改变标题提醒学生老师正在输入
function remberTeacherHander(data)
{
document.title=data.Message;
}
// 向面板中增加新接收到的消息
// data数据结构 type 1代表文字信息 2代表语音的路径
function addMsgToZhuban(data) {
var Message =data.Message;
var NickName=data.NickName;
var HeadPic =data.HeadPic;
var Type    =data.Type;
var	 wk =document.getElementsByClassName("weke_zhibo")[0];
var item = wk.getElementsByClassName("wk_item")[0],
send = wk.getElementsByClassName("wk_senditem")[0],
stu_item = wk.getElementsByClassName("stu_item")[0],//学生
stu_send = wk.getElementsByClassName("stu_senditem")[0],//学生
danmu =  wk.getElementsByClassName("danmu")[0];
var content =wk.getElementsByClassName("content")[0];			
var sect = document.createElement("section"),
//str = ["img/replaceable/photo.png","自然而然",item.value],//头像、用户名、字段
strin='<div><img src='+HeadPic+' />'+
'</div><div><p>'+NickName+'</p>'+
'<div>'+Message+'</div></div>';
sect.innerHTML = strin;
content.appendChild(sect);
item.value = "";
}
//向主板中添加音频信息 接受信息
function addMp3ToZhuban(data)
{	
var Message =data.Message;
var NickName=data.NickName;
var HeadPic =data.HeadPic;
var Type    =data.Type;
var Time    =data.Time;
var T       =data.T;
var number  =data.number;
var sort    =data.sort;
//通过message=>serverId mediaId下载音频到本地
wx.downloadVoice({
    serverId: Message, // 需要下载的音频的服务器端ID，由uploadVoice接口获得
    isShowProgressTips: 0, // 默认为1，显示进度提示
    success: function (res) {
        var localId = res.localId; // 返回音频的本地ID
        var wk =document.getElementsByClassName("weke_zhibo")[0];
        var content =wk.getElementsByClassName("content")[0];
			sect = document.createElement("section"),
			strin='<div><img src='+HeadPic+' />'+
			'</div><div><p>'+NickName+'</p>'+
			'<div style="width:'+Time+'%" class="s_div talk '+"aready_talk" +'"'+
			'onclick ="javascript:this.getElementsByTagName(&#34;aside&#34;)[0].style.display=&#34;none&#34;"><div class=""></div>'+
			'<article><input type="radio" name="audio" onclick="audio1(this)" onfocusout="audio2(this)" number="'+number+'" sort="'+sort+'" ><audio name="local"  src="'+localId+'"controls="controls"></audio></article>'+
			'<aside></aside><div><span>'+T+"</span>''</div></div></div>";
			sect.innerHTML = strin;
			content.appendChild(sect);
    }
});
//str = ["img/replaceable/photo.png","自然而然",t,"aready_talk","viedo/吉田潔-Tvエンディングテーマ.mp3"],
}
// 向弹幕中添加接收到的信息
function addMsgToDanmu(data)
{
var wk =document.getElementsByClassName("weke_zhibo")[0],
    danmu =  wk.getElementsByClassName("danmu")[0],
	add_danmu = document.getElementById("add_danmu"),
	item1 = wk.getElementsByClassName("wk_item")[1], //此处链接到第二行定义的wk
	danmu_inp = danmu.getElementsByTagName("input")[0],
	float_art = document.getElementsByClassName("float_art")[0],
	float_sec = float_art.getElementsByTagName("section");
	
	


	var add_div = add_danmu.getElementsByTagName("div")[0],
		add_sec = add_danmu.getElementsByTagName("section"),	
	day = new Date(),
		//用户数据   头像 、用户名  内容  年月日 时分秒
		
				sect = document.createElement("section"),
				strin='<div><img src='+data.HeadPic+' />'+
					'</div><div><p>'+data.NickName+'</p>'+
					'<div>'+data.Message+'</div>'+
					'<p><span>'+data.TimeY+'</span><span>'+data.TimeM+'</span></p></div>';
				sect.innerHTML = strin;
			add_div.insertBefore(sect,add_sec[0]);//弹幕
				//窗口弹幕部分
			var sect2=document.createElement("section"),
				strin2 = '<div><img src="'+data.HeadPic+'" />'+
						 '</div><p>'+data.Message+'</p>';
				sect2.innerHTML = strin2;
				float_art.removeChild(float_sec[0]);
				float_art.appendChild(sect2)		
	//send.style.display =  'none';
	danmu.style.display =  'flex';		
}

window.addEventListener("load",function(){
   //如果是主播进行授权处理
   if(loginUser==roomUser)
   {
   if(!localStorage.rainAllowRecord || localStorage.rainAllowRecord!='true'){
   setTimeout(function(){
        //调用微信jssdk语音开始	
		alert('开始语音授权测试1');
        wx.startRecord({
            success: function(){
                localStorage.rainAllowRecord = 'true';
				wx.stopRecord();
            },
            cancel: function () {
                alert('用户拒绝授权录音');
            }
        });
        },300);
   }
   }
	var htmfontsi = document.body.clientWidth,
	htmfontsize = parseFloat(htmfontsi)/750*100;
	/*if(document.getElementsByClassName("weke_zhibo")){*/
		(function(){
			var _mm=document.getElementsByClassName("float_art")[0],
			_mm_len =_mm.getAttribute("value"),
			_section = _mm.getElementsByTagName("section"),
			value_null = _mm.getElementsByClassName("value_null");
			for(var _mm_i=2; _mm_i>(3- _mm_len);_mm_i--){
				value_null[_mm_i].setAttribute("class","");
			}
		})()	
		//滑动出现返回当前位置按钮添加定位id
		var scroll_show = document.getElementsByClassName("scroll_show")[0],
			art_scroll = document.getElementsByClassName("art_scroll")[0],
			art_scroll_time,
			art_scroll_t = 	setTimeout(function(){
								if( art_scroll_time == art_scroll.scrollTop){
									scroll_show.setAttribute("class","scroll_show");
								}
							},2000);
			art_scroll.onscroll=function(){
				clearTimeout(art_scroll_t);
				art_scroll_t = 	setTimeout(function(){
									if( art_scroll_time == art_scroll.scrollTop){
										scroll_show.setAttribute("class","scroll_show");
									}
								},2000);
				art_scroll_time = this.scrollTop;
				if(!this.className.indexOf("show_").length){
					scroll_show.setAttribute("class","scroll_show show_");
				}
			}
		//回到当前播放位置
	var scroll_choose =scroll_open,
			font_size = parseFloat(document.body.clientWidth)/750*100,
			srcoll_begin = font_size*3.88;
		on(scroll_show,"click",function(){
			scroll_choose();
		})
		function scroll_open(){
			if(document.getElementById("_back")){
				var _back =document.getElementById("_back");
					art_scroll.scrollTop = _back.parentNode.parentNode.parentNode.parentNode.offsetTop+2*font_size;	
			}
		}
		function scroll_close(){
			if(document.getElementById("_back")){
				var _back =document.getElementById("_back");
					art_scroll.scrollTop = _back.parentNode.parentNode.parentNode.parentNode.offsetTop-srcoll_begin+3*font_size;				
			}
		}
		on(document.getElementsByClassName("class_know")[0].getElementsByClassName("close")[0],"click",function(){
				scroll_choose = scroll_close;
		})


		var wk =document.getElementsByClassName("weke_zhibo")[0],
		foot = wk.getElementsByClassName("footer")[0];
		//关闭讲课须知
		var class_know= wk.getElementsByClassName("class_know")[0],
		w_close = class_know.getElementsByClassName("close")[0];
		on(w_close,"click",function(){
			class_know.style.display="none";
		})
		//老师发送信息
		var item = wk.getElementsByClassName("wk_item")[0],
		send = wk.getElementsByClassName("wk_senditem")[0],
		stu_item = wk.getElementsByClassName("stu_item")[0],//学生
		stu_send = wk.getElementsByClassName("stu_senditem")[0],//学生
		danmu =  wk.getElementsByClassName("danmu")[0];
		on(item ,"input",function(){
			if(item.value){
			send.style.display =  'block';
			danmu.style.display =  'none';
			}else{
			send.style.display =  'none';
			danmu.style.display =  'flex';
			}
		})
		on(send,"click",function(){
			var content =wk.getElementsByClassName("content")[0];
			if(item.value){
				var sect = document.createElement("section"),
				//str = ["img/replaceable/photo.png","自然而然",item.value],//头像、用户名、字段
				strin='<div><img src='+headPic+' />'+
				'</div><div><p>'+nickName+'</p>'+
				'<div>'+item.value+'</div></div>';
				sect.innerHTML = strin;
				content.appendChild(sect);
				//房间ID,用户ID,用户发送信息,发送类型
			    //1:代表文字信息 2:代表是语音文件路径
			    var Data={};
			    Data['RoomUserId']=roomUser;
				Data['RoomId'] =roomId;
			    Data['UserId'] =loginUser;
			    Data['Message']=item.value;
			    Data['Type']   =1;
			    // 向server端发送ajax请求
			    $.post(ajaxMsgUrl,Data,function (data) {
			    },'json');
				item.value = "";
			}
			send.style.display =  'none';
			danmu.style.display =  'flex';
			//房间ID,用户ID,用户发送信息,发送类型
			//1:代表文字信息 2:代表是语音文件路径
		
		})
		//点击录音
		var	luyin=wk.getElementsByClassName("luyin")[0],
		luyin_b = luyin.getElementsByTagName("button")[0],
		luyin_aside = luyin.getElementsByTagName("aside")[0], 
		w_time=luyin.getElementsByClassName("w_time")[0],
		t_w_time;
		//如果是主播进行授权处理
		if(loginUser==roomUser)
        {
		if(!localStorage.rainAllowRecord || localStorage.rainAllowRecord!='true'){
        setTimeout(function(){
        //调用微信jssdk语音开始	
		alert('开始语音授权测试2');
        wx.startRecord({
            success: function(){
                localStorage.rainAllowRecord = 'true';
				wx.stopRecord();
            },
            cancel: function () {
                alert('用户拒绝授权录音');
            }
        });
        },300);
        }
        }
		//点击开始录音
		on(luyin_b,"touchstart",function(event){
			luyin_aside.style.display ="block";
			luyin_b.style.backgroundColor="#999";

			var Data={};
			Data['RoomUserId']=roomUser;
		    Data['RoomId'] =roomId;
			Data['UserId'] =loginUser;
			Data['Message']="老师正在输入..";
			Data['Type']   =3;
			// 向server端发送ajax请求
			$.post(ajaxMsgUrl,Data,function (data) {
			    },'json');

		var m = 0;
		w_time.innerHTML = m;
		t_w_time=setInterval(function(){
		m += 1;
		w_time.innerText = m},1000)	
	    event.preventDefault();
        START = new Date().getTime();
        recordTimer = setTimeout(function(){
        //调用微信jssdk语音开始	
        wx.startRecord({
            success: function(){
                localStorage.rainAllowRecord = 'true';
            },
            cancel: function () {
                alert('用户拒绝授权录音');
            }
        });
        },300);
		})
	    //结束录音
	    on(luyin_b,"touchend",function(event){
	    number=document.getElementsByClassName("page")[0].innerHTML;	
	    var Data={};
		Data['RoomUserId']=roomUser;
		Data['RoomId'] =roomId;
	    Data['UserId'] =loginUser;
		Data['Message']="{$Title}";
		Data['Type']   =3;
		// 向server端发送ajax请求
		$.post(ajaxMsgUrl,Data,function (data) {
		},'json');	
	    clearInterval(t_w_time);
	    var t = w_time.innerText,
	    	time = t/1.2+15;
		luyin_aside.style.display ="none";	
		luyin_b.style.backgroundColor="";
		//只有当录音时间大于2秒的时候记录下来防止用户一直点
		if(t>2)	
		{
		//结束录音得到本地音频的id	    	
	   	wx.stopRecord({
        success: function (res) {
        var localId = res.localId;
            //str=[用户头像url,username,time,是否已播放,音频url]
            var content =wk.getElementsByClassName("content")[0],
			sect = document.createElement("section"),
			strin='<div><img src='+headPic+' />'+
			'</div><div><p>'+nickName+'</p>'+
			'<div style="width:'+time+'%" class="s_div talk '+"aready_talk" +'"'+
			'onclick ="javascript:this.getElementsByTagName(&#34;aside&#34;)[0].style.display=&#34;none&#34;"><div class=""></div>'+
			'<article><input type="radio" name="audio" onclick="audio1(this)" onfocusout="audio2(this)" number="'+number+'" sort="'+sort+'" /><audio name="local"  src="'+localId+'"controls="controls"></audio></article>'+
			'<aside></aside><div><span>'+t+"</span>''</div></div></div>";
			sect.innerHTML = strin;
			content.appendChild(sect);
            //上传本地音频到微信服务器上
            uploadVoice(time,localId,t);
        }
       });
       }
       else{
       	alert("录音时间过短,请重新录音");
       }	 			
	})
	//学生发送信息
	var add_danmu = document.getElementById("add_danmu"),
	item1 = wk.getElementsByClassName("wk_item")[1],
	danmu_inp = danmu.getElementsByTagName("input")[0],
	float_art = document.getElementsByClassName("float_art")[0],
	float_sec = float_art.getElementsByTagName("section");
	on(item1,"click",function(){
		danmu_inp.click();	
		//苹果键盘fixed问题方案
	})
	on(danmu_inp,"click",function(){
		stu_item.focus();
		if(!!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)){
			setTimeout(function () { 
	            stu_item.scrollIntoViewIfNeeded();    
		    }, 300);
		}
	})
	on(stu_send,"click",function(){
		var add_div = add_danmu.getElementsByTagName("div")[0],
			add_sec = add_danmu.getElementsByTagName("section"),
		day = new Date(),
	//用户数据   头像 、用户名  内容  年月日 时分秒
		str = ["img/replaceable/photo.png","自然而然",stu_item.value,day.getFullYear()+"-"+(day.getMonth()+1)+"-"+day.getDate(),day.getHours()+":"+day.getMinutes()+":"+day.getMinutes()];
		if(stu_item.value){
			var sect = document.createElement("section"),
			strin='<div><img src='+headPic+' />'+
			'</div><div><p>'+nickName+'</p>'+
			'<div>'+stu_item.value+'</div>'+
			'<p><span>'+str[3]+'</span><span>'+str[4]+'</span></p></div>';
			sect.innerHTML = strin;
			add_div.insertBefore(sect,add_sec[0]);//弹幕
			var Data={};
			Data['RoomUserId']=roomUser;
			Data['RoomId'] =roomId;
			Data['UserId'] =loginUser;
			Data['Message']=stu_item.value;
			Data['Type']   =1;
			// 向server端发送ajax请求
			$.post(ajaxMsgUrl,Data,function (data) {
			},'json');
			//窗口弹幕部分
			var sect2=document.createElement("section"),
			strin2 = '<div><img src="'+headPic+'" />'+
			'</div><p>'+stu_item.value+'</p>';
			stu_item.value = "";
			sect2.innerHTML = strin2;
			float_art.removeChild(float_sec[0]);
			float_art.appendChild(sect2)
		}
		send.style.display =  'none';
		danmu.style.display =  'flex';
	})
	//切换状态
		var check = foot.getElementsByClassName("check")[0];
		on(check,"click",function(){
			send.style.display =  'none';
			danmu.style.display =  'flex';
		})
//弹幕
	var danmu_li = document.getElementsByClassName("danmu_li")[0],
	close = danmu_li.getElementsByClassName("close")[0],
	add = document.getElementById("add_danmu"),
	add_div = add.getElementsByTagName("div")[0];
	var danmu = document.getElementsByClassName("danmu")[0],
	d_inpu = danmu.getElementsByTagName("input")[0];
	var add_t = ad;
	on(close,"click",function(){
		d_inpu.click();
	})
	on(add,"scroll",function(){
		if(this.clientHeight + this.scrollTop +5 >= this.scrollHeight){
		add_t();
		add_t = function(){};
		}
	})
	//直播间弹幕数据							
		function ad(){
			add_div.setAttribute("class","add1");
		}

    })
	function on(argu,method,fun){
	argu.addEventListener(method,fun);
	}
	//音频播放点击事件
	function audio1(aud){
	var n = aud.parentNode,
		j = n.parentNode,
		k = n.previousSibling,
		l = n.nextSibling.nextSibling.getElementsByTagName("span")[0];
	var m =aud.nextSibling;
	var judge_play=function(){
			m.onended=function(){
				n.previousElementSibling.classList.remove("talking");
				if(document.getElementsByClassName("content")[0].getElementsByTagName("input")[Number(aud.getAttribute("sort"))]){
					var ppp=document.getElementsByClassName("content")[0].getElementsByTagName("input")[Number(aud.getAttribute("sort"))];
						ppp.checked=true;
						setTimeout(function(){
							if(ppp.checked){
								audio1(ppp);
							}
						},1000);
				}
			}			
		};
    //播放图片Number
    function _play(_number){
	var	_page=document.getElementsByClassName("page"),
		hdp_banner = document.getElementsByClassName("hdp_banner")[0],
		hdp_art = hdp_banner.getElementsByTagName("article")[0];
	var	hdp_img_length = hdp_art.getElementsByTagName("img").length;
	 	if(_number != _page[0].innerHTML){
			_page[0].innerHTML = _number;
			hdp_art.style.transform = "translateX(-"+(_number-1)/hdp_img_length*100+"%)";
	 	}
};
   //回到当前位置
   var judge_img_length =document.getElementsByClassName("hdp_banner")[0].getElementsByTagName("img").length;
        if(judge_img_length && judge_img_length > 1){
	        	if(document.getElementById("_back") && document.getElementById("_back").getAttribute("sort")!=aud.getAttribute("sort")){
				var removeid = document.getElementById("_back");
					removeid.checked = false;
					removeid.setAttribute("id","");
					removeid.setAttribute("class","");
					removeid.nextElementSibling.pause();
					removeid.parentNode.previousElementSibling.setAttribute("class","");
				}
				aud.setAttribute("id","_back");		
			}else{
	        	if(document.getElementById("_back") && document.getElementById("_back").getAttribute("sort")!=aud.getAttribute("sort")){
				var removeid = document.getElementById("_back");
					removeid.checked = false;
					removeid.setAttribute("id","");
					removeid.setAttribute("class","");
					removeid.nextElementSibling.pause();
					removeid.parentNode.previousElementSibling.setAttribute("class","");
				}
				aud.setAttribute("id","_back");
		}

	   // console.log(m);
		//console.log(n.previousSibling);
	//console.log(m.getAttribute('name'));
	//m.currentTime = 0;
	//播放本地音频
	//获取音频ID
	if(m.getAttribute('name') =="local"){

        var voiceId=m.src;
        //停止播放上一个正在播放的音频
		if(OldLocalid!='')
		{
		
            if(k==OldVoice&& $(k).attr('class') == "talking")
			{
			clearTimeout(timeout);
			wx.stopVoice({
            localId: OldLocalid // 需要停止的音频的本地ID，由stopRecord接口获得
            });	
			$(OldVoice).removeClass("talking");
			return;
			}
			else
			{
			clearTimeout(timeout);
			wx.stopVoice({
            localId: OldLocalid // 需要停止的音频的本地ID，由stopRecord接口获得
            });	
			$(OldVoice).removeClass("talking");	
			}
		}
        OldLocalid=voiceId;
        OldVoice=k;
		//打印本地音频
		//alert(voiceId);
		$(j).addClass("talked");
		//当还在播放的时候在点击播放停止
		if($(k).attr('class') == "talking"){
			wx.stopVoice({
            localId: voiceId // 需要停止的音频的本地ID，由stopRecord接口获得
        });
		$(k).removeClass("talking");
		clearTimeout(timeout);
		}
		//当不是播放状态的时候点击播放
		else{
			timeout=false;
			$(k).addClass("talking");
			wx.playVoice({
            localId: voiceId // 需要播放的音频的本地ID，由stopRecord接口获得
            });
		    timeout=setTimeout(function(){	
		    $(k).removeClass("talking");
		    if(document.getElementsByClassName("content")[0].getElementsByTagName("input")[Number(aud.getAttribute("sort"))]){
			var _num = Number(aud.getAttribute("sort"));
			setTimeout(function(){
			//总结定时器	
			document.getElementsByClassName("content")[0].getElementsByTagName("input")[_num].click();
			},1000);
			}
		    },l.innerText*1000)
		}	
	}
	//未过3天可以从微信服务器下载资源
	else if(m.getAttribute('name') =="amr")
	{
      var media=m.src.substring(35);
	  if(OldLocalid!='')
		{
			if(k==OldVoice&& $(k).attr('class') == "talking")
			{
			clearTimeout(timeout);
			wx.stopVoice({
            localId: OldLocalid // 需要停止的音频的本地ID，由stopRecord接口获得
            });	
			$(OldVoice).removeClass("talking");
			return;
			}
			else
			{
			clearTimeout(timeout);
			wx.stopVoice({
            localId: OldLocalid // 需要停止的音频的本地ID，由stopRecord接口获得
            });	
			$(OldVoice).removeClass("talking");	
			}			
		}
		OldVoice=k;
	  wx.downloadVoice({
        serverId: media, // 需要下载的音频的服务器端ID，由uploadVoice接口获得
        isShowProgressTips: 0, // 默认为1，显示进度提示
        success: function (res) {
        voiceId = res.localId; // 返回音频的本地ID 
        OldLocalid=voiceId;
        $(j).addClass("talked");
		//当还在播放的时候在点击播放停止
		if($(k).attr('class') == "talking"){
			wx.stopVoice({
            localId: voiceId // 需要停止的音频的本地ID，由stopRecord接口获得
        });
		$(k).removeClass("talking");
        clearTimeout(timeout);
		}
		//当不是播放状态的时候点击播放
		else{
			timeout=false;
			k.classList.add("talking");
			wx.playVoice({
            localId: voiceId // 需要播放的音频的本地ID，由stopRecord接口获得
            });
		    timeout=setTimeout(function(){	
		    $(k).removeClass("talking");
		    if(document.getElementsByClassName("content")[0].getElementsByTagName("input")[Number(aud.getAttribute("sort"))]){
			var _num = Number(aud.getAttribute("sort"));
			setTimeout(function(){
			document.getElementsByClassName("content")[0].getElementsByTagName("input")[_num].click();
			},1000);
			}
		    },l.innerText*1000)
		}
        }
        });   
	}
	//播放远程音频
	else{
		var judge_img_length =document.getElementsByClassName("hdp_banner")[0].getElementsByTagName("img").length;
        if(judge_img_length && judge_img_length > 1){
	      		if(m.paused){
				aud.setAttribute("class","player")
					m.play();
					n.parentNode.classList.add("talked");
					n.previousElementSibling.classList.add("talking");
					judge_play(aud,n,m);
					judge_play = null;
					var _number=aud.getAttribute("number");
					var _player = document.getElementsByClassName("player");
					if(_player.length>2){
						for(var i =0;i<_player.length;i++){
							if(_player[i].getAttribute("sort") != aud.getAttribute("sort")){
								_player[i].nextElementSibling.pause();
								_player[i] = false;
								_player[i].parentNode.previousElementSibling.setAttribute("class","");
							}
						}
					}
					_play(_number);
			}else{
				aud.checked = false;
				m.pause();
				n.previousElementSibling.setAttribute("class","");
			}
		}else{
	        	if(m.paused){
				aud.setAttribute("class","player")
					m.play();
					n.parentNode.classList.add("talked");
					n.previousElementSibling.classList.add("talking");
					judge_play(aud,n,m);
					judge_play = null;
					var _number=aud.getAttribute("number");
					var _player = document.getElementsByClassName("player");
					if(_player.length>2){
						for(var i =0;i<_player.length;i++){
							if(_player[i].getAttribute("sort") != aud.getAttribute("sort")){
								_player[i].nextElementSibling.pause();
								_player[i] = false;
								_player[i].parentNode.previousElementSibling.setAttribute("class","");
							}
						}
					}
			}else{
				aud.checked = false;
				m.pause();
				n.previousElementSibling.setAttribute("class","");
			}
		}
	}

	var _number = aud.getAttribute("number"),
	_page=document.getElementsByClassName("page"),
	hdp_banner = document.getElementsByClassName("hdp_banner")[0],
	hdp_art = hdp_banner.getElementsByTagName("article")[0],
	hdp_img_length = hdp_art.getElementsByTagName("img").length;
 	if(_number != _page[0].innerHTML){
		_page[0].innerHTML = _number;
		hdp_art.style.transform = "translateX(-"+(_number-1)/hdp_img_length*100+"%)";
 	}
    }
    //失焦函数
    function audio2(aud){	
	var n = aud.parentNode;
	var m =aud.nextSibling;
		m.currentTime = 0;
		if(m.getAttribute('name') =="local")
		{
		//clearTimeout(timeout);
		n.previousSibling.classList.remove("talking");
       // wx.stopVoice({
        //localId: m.src // 需要停止的音频的本地ID，由stopRecord接口获得
       // });
		}
		else if(m.getAttribute('name') =="amr")
		{
        //var media=m.src.substring(35);
	    //var voiceId
        //timeout=true;
        //
        //clearTimeout(timeout);
        n.previousSibling.classList.remove("talking");
	    //wx.downloadVoice({
        //serverId: media, // 需要下载的音频的服务器端ID，由uploadVoice接口获得
        //isShowProgressTips: 0, // 默认为1，显示进度提示
        //success: function (res){
        //wx.stopVoice({
        //localId:res.localId  // 需要停止的音频的本地ID，由stopRecord接口获得
        //});	
        //}
		//});
	    }
	    //播放媒体标签判断
		else
		{	
	   	var n = aud.parentNode;
	    var m =aud.nextElementSibling;
		m.currentTime = 0;
		aud.checked = false;
	   	m.pause();
		n.previousElementSibling.setAttribute("class","");
		aud.setAttribute("class","")
	    }
    }
    //上传录音
    function uploadVoice(time,mp3id,t){
    //调用微信的上传录音接口把本地录音先上传到微信的服务器
    //不过，微信只保留3天，而我们需要长期保存，我们需要把资源从微信服务器下载到自己的服务器
    //加载loading
    //$("#loading").attr('class','aside');
    wx.uploadVoice({
        localId:mp3id, // 需要上传的音频的本地ID，由stopRecord接口获得
        isShowProgressTips: 0, // 默认为1，显示进度提示
        success: function (res) {
            //即时通讯进行群发
            //房间ID,用户ID,用户发送信息,发送类型
			//1:代表文字信息 2:代表是语音文件路径
			var Data={};
			Data['RoomId'] =roomId;
			Data['UserId'] =loginUser;
			Data['Message']=res.serverId;
			Data['Time']   =time;
			Data['T']      =t;
			Data['Type']   =2;
			if(document.getElementsByClassName("page").length)
			{
			Data['number'] =document.getElementsByClassName("page")[0].innerHTML;
		    }
		    Data['sort'] =sort;
		    sort++;
			//向server端发送ajax请求
			$.post(ajaxMsgUrl,Data,function (data) {
			},'json');
            //把录音在微信服务器上的id（res.serverId）发送到自己的服务器供下载。
            $.ajax({
                url: 'http://mg.bonayiyou.com/index/upload/uploadvoice',
                type: 'post',
                data: res,
                dataType: "json",
                success: function (data) {
                //去除loading
                //$("#loading").attr('class','aside none');
                },
                error: function (xhr, errorType, error) {
                    console.log(error);
                }
            });
            //$("#loading").attr('class','aside none');

        }
    });
}
        function follow(obj)
        {
          	var data={};
            data['teacher_id']="{$RoomUserId}";
            data['user_id']   ="{$UserId}";
			if(obj.checked)
			{
               url="http://mg.bonayiyou.com/index/home/add_follow";
               $.post(url,data,function(e){},"JSON");
			}
			else
			{
   
               url="http://mg.bonayiyou.com/index/home/del_follow";
               $.post(url,data,function(e){},"JSON");   
			}

        }
        function click_back(){
			var content = document.getElementsByClassName("content")[0],
				_input = content.getElementsByTagName("input");
				for(var i=0;i<_input.length;i++){
					if(_input[i].checked){
						_input[i].setAttribute("id","_back");
						(function(i){
							setTimeout(function(){
								_input[i].setAttribute("id","");
							},100)
						})(i);
						return;
					}
				}		
		}
        window.addEventListener("load",function(){       
		//判断是否已经直播过
		if(!+document.getElementsByClassName("weke_head_asd")[0].getAttribute("value")){
			var weke_h_p = document.getElementsByClassName("weke_head_asd")[0].getElementsByTagName("p")[0];
			    //开始直播点击事件
				on(weke_h_p,"click",function(){
				if(this.classList ==""){						
				var Data={};
		        Data['RoomUserId']=roomUser;
		        Data['RoomId'] =roomId;
	            Data['UserId'] =loginUser;
		        Data['Message']="同学们直播开始了哦";
		        Data['Type']   =1;
		        // 向server端发送ajax请求
		        $.post(ajaxMsgUrl,Data,function (data) {
		        },'json');
		        $.post("http://mg.bonayiyou.com/index/home/beg_end?type=1&room_id="+roomId,{data:"1"},function(){
		        },"JSON");
					this.classList.add("playing");
						this.classList.add("playing");

					}else{
				var r=confirm("确定结束课程吗?");
				if(r==true)
				{		
				var Data={};
		        Data['RoomUserId']=roomUser;
		        Data['RoomId'] =roomId;
	            Data['UserId'] =loginUser;
		        Data['Message']="同学们直播结束了,欢迎收看其他课程";
		        Data['Type']   =1;
		        // 向server端发送ajax请求
		        $.post(ajaxMsgUrl,Data,function (data) {
		        },'json');
		        $.post("http://mg.bonayiyou.com/index/home/beg_end?type=2&room_id="+roomId,{data:"1"},function(){
		        },"JSON");

						this.setAttribute("class","played");
				}		
					}

				})
		}else{                        
			if(+document.getElementsByClassName("weke_head_asd")[0].getAttribute("value") == 1){
				var weke_h_p = document.getElementsByClassName("weke_head_asd")[0].getElementsByTagName("p")[0];
					weke_h_p.classList.add("playing");
				on(weke_h_p,"click",function(){
				var Data={};
		        Data['RoomUserId']=roomUser;
		        Data['RoomId'] =roomId;
	            Data['UserId'] =loginUser;
		        Data['Message']="同学们直播结束了,欢迎收看其他课程";
		        Data['Type']   =1;
		        // 向server端发送ajax请求
		        $.post(ajaxMsgUrl,Data,function (data) {
		        },'json');
		        $.post("http://mg.bonayiyou.com/index/home/beg_end?type=2&room_id="+roomId,{data:"1"},function(){
		        },"JSON");

				this.setAttribute("class","played");
				})
			}
            else{
				document.getElementsByClassName("weke_head_asd")[0].getElementsByTagName("p")[0].classList.add("played");
			}
		}
	    
		function on(argu,method,fun){
			argu.addEventListener(method,fun);
		}
	    var hdp_banner = document.getElementsByClassName("hdp_banner")[0],
			hdp_art = hdp_banner.getElementsByTagName("article")[0];
		var	hdp_img = hdp_art.getElementsByTagName("img"),
			hdp_img_length = hdp_img.length,
			_page = document.getElementsByClassName("page");
		if(hdp_art.getElementsByTagName("img").length){
			hdp_art.style.width = hdp_img_length+"00%";
			if(hdp_img_length ==1){
				hdp_banner.getElementsByTagName("aside")[0].style.display ="none";
				on(hdp_art,"click",function(){
					if(hdp_img[0].naturalWidth<hdp_img[0].naturalHeight){
						hdp_img[0].style.height = "100%";
					}else{
						hdp_img[0].style.width = "100%";
					}
				})
			}else{
				on(_page[1],"click",function(){
					pages(-1);
				})
				on(_page[2],"click",function(){
					pages(1);
				})
				function pages(i){
					var m = Number(_page[0].innerHTML)-1+i;
					if( m >=0 && m <hdp_img_length){
						_page[0].innerHTML = m+1;
						hdp_art.style.transform = "translateX(-"+m/hdp_img_length*100+"%)";
					}else{
					}
				}
				var scroll_div = document.getElementsByClassName("scroll_div")[0],
					xl,xr;
					if (hdp_img.length >= 2) {
						on(hdp_art,"touchstart",function(event){
							xl = event.changedTouches[0].clientX;
						})
						on(hdp_art,"touchend",function(){
							var xr =event.changedTouches[0].clientX;
							if (xr - xl > 50) {
								 _page[1].click();
							} else if (xr - xl < -50) {
								 _page[2].click();
							}else{
								if(scroll_div.getAttribute("id")){
									scroll_div.setAttribute("id","");
								}else{
									scroll_div.setAttribute("id","transbig");
									for(var i =0 ; i<hdp_img.length;i++){
										if(hdp_img[i].naturalWidth<hdp_img[i].naturalHeight){
											hdp_img[i].style.height = "100%";
										}else{
											hdp_img[i].style.width = "100%";
										}
									}
								}
							}
						})
					}
			}
			
		}else{
			hdp_banner.style.display = "none";
			hdp_banner.nextElementSibling.style.paddingTop = "0.88rem";
		}
	})
    if("{$follow}")
    {
    document.getElementById("guanzhu").checked="1";
    }
	//离开页面时关掉正在播放的
	window.onbeforeunload=function(){
    wx.stopVoice({
    localId: OldLocalid // 需要停止的音频的本地ID，由stopRecord接口获得
    });
    }
		</script>
    }
